//
//  CZStatusViewModel.swift
//  CZWeibo
//
//  Created by 周振兰 on 16/5/17.
//  Copyright © 2016年 周振兰. All rights reserved.
//

import UIKit

class CZStatusViewModel: NSObject {
    //单例
    static let shardIntance : CZStatusViewModel = CZStatusViewModel()
    
    ///私有构造函数  ???
  private  override init() {
        super.init()
    }
    
  ///加载微博数据
    func loadStatus(loadStatusCallback:(statuses: [CZStatus]?,error:NSError?)->())
    {
    guard let access_token = CZUserAccountViewModel.shareIntance.userAccount?.access_token
        else
    {
        CZPrint(items: "access_token没有值，不去请求")
        return
        }
        
        let urlString = "2/statuses/home_timeline.json"
        //参数
        let parameters = [
            "access_token" : access_token
       
          
        ]
         //请求
        CZNetworkTool.shareaIntance.request(RequestMethod.GET, URLString: urlString, parameters: parameters, success: { (_, responseObject) -> Void in
            
            //返回数据是字典
            if let result = responseObject as? [String:AnyObject]
            {
                //获取到所有的微薄数据
                if let statuses = result["statuses"] as?[[String:AnyObject]]
                {
                    //定义空数组存放转好的微薄数据
                    var statusArray = [CZStatus]()
                    //遍历数组，取出每一个微薄字典
                    for dict in statuses
                    {
                        //字典转模型
                        let statusModel = CZStatus(dic: dict)
                        //将模型添加到数组中
                        statusArray.append(statusModel)
                    }
//                    CZPrint(items: "微薄模型数组：\(statusArray)")
                    //将模型给调用的人
                    loadStatusCallback(statuses: statusArray, error:nil)
    
                }
                
            }
            
            
            CZPrint(items: "模型数据\(responseObject)")
            }) { (_, error) -> Void in
                
//                CZPrint(items: "获取微薄数据失败：\(error)")
                loadStatusCallback(statuses: nil, error: error)
        }
    }
    
}
